﻿using System;
using System.Collections.Generic;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the fixed tax sales rule entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   | - Amount
    ///   | - Flags
    ///   + Base
    ///    | - Name
    ///    | - CommonFlags
    ///    | - ValidFrom
    ///    | - ValidTo
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    + Keys
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class FixedTaxRuleEntity : SalesRuleEntity
    {
        #region Constants

        /// <summary>
        /// The specification system code.
        /// </summary>
        public const string CODE = "FIXED_TAX";

        #endregion

        #region Properties

        /// <summary>
        /// Gets the global unique identifier of a rule handler.
        /// </summary>
        public override Guid HandlerID
        {
            get
            {
                return Guid.Parse("40f78bea-4e16-4bb9-9080-2d14890a337b");
            }
        }

        /// <summary>
        /// Gets the a rule priority.
        /// </summary>
        public override int Priority
        {
            get
            {
                return 50;
            }
        }

        /// <summary>
        /// Gets the rule system code.
        /// </summary>
        public override string Code
        {
            get
            {
                return CODE;
            }
        }

        /// <summary>
        /// Gets or sets the amount.
        /// </summary>
        public virtual decimal Amount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the common flags.
        /// </summary>
        public virtual FixedTaxRuleFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Extended)
            {
                obj["Amount"] = Amount;
                obj["Flags"] = Flags.ToString();
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class FixedTaxRuleMapping : JoinedEntityMapping<FixedTaxRuleEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public FixedTaxRuleMapping()
        {
            Table("fwk_FixedTaxRules");
            Key(m =>
            {
                m.NotNullable(true);
                m.Column("TaxID");
                m.ForeignKey("FK_fwk_FixedTaxRules_fwk_SalesRules");
            });
            Property(x => x.Amount, m =>
            {
                m.Precision(19);
                m.Scale(4);
                m.NotNullable(true);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
        }
    }
}
